package com.gopay.cron.event.manager;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gopay.common.datasource.DBType;
import com.gopay.common.datasource.DbContextHolder;
import com.gopay.common.domain.report.Event;
import com.gopay.common.event.EventState;
import com.gopay.common.report.dao.MqEventInfoDAO;
import com.gopay.cron.event.handler.UpdateEventDefaultHandler;
import com.gopay.cron.service.trans.ReportConsoleTransQueryService;
import com.gopay.cron.web.vo.TransQueryResultVO;



@Service("updateEventHandlerManager")
public class UpdateEventHandlerManager implements EventHandlerManager{
	 private Logger log = LoggerFactory.getLogger(UpdateEventDefaultHandler.class);
    @Resource 
    private ReportConsoleTransQueryService reportConsoleTransQueryService;
    @Resource(name="mqEventInfoDAO")
    private MqEventInfoDAO mqEventInfoDAO;
    

    @Transactional(value="reportTransactionManager")
    public void saveOrUpdateOrderAndEvent(List<TransQueryResultVO> transQueryResultList, Event e) {
        DbContextHolder.setDbType(DBType.DS_REPORT.value);
        
        for (TransQueryResultVO t : transQueryResultList) {
            this.saveOrUpdateOrderAndEvent(t, e);
        }
        //reportConsoleTransQueryService.updateTransQueryResultList(transQueryResultList);
    }
    
   
    public void saveOrUpdateOrderAndEvent(TransQueryResultVO transQueryResultVO, Event e){
        boolean updateResult = reportConsoleTransQueryService.updateTransQueryResult(transQueryResultVO);
        Event result = mqEventInfoDAO.find(Event.class, e.getId());
        if(null != result){
            if(updateResult){
                result.setStat(EventState.SUCCESS.value);
            }else{
                result.setStat(EventState.FAIL.value);
            }
            result.setLastUpdTs(new Date());
            mqEventInfoDAO.update(result);
        }
    }

}
